/**
 * Created with JetBrains PhpStorm.
 * User: vander
 * Date: 5/9/13
 * Time: 9:33 PM
 * To change this template use File | Settings | File Templates.
 */
document.addEventListener("deviceready", onDeviceReady, false);

$j("#collection").on("pageinit", function(event){
    Global.collections = new Collections();

//    $j.getJSON("data/collections.json", function(data){
//        Global.collections.load(data);
//    });


    $j("#favorite").page();
    $j("#bookmark").page();

});

$j("#store").on("pageinit", function(event){
    Global.store = new Store();
    $j.getJSON(Static.serverPath + "getStoreCollection.php", function(data){
        Global.store.load(data);
    });

    $j("#downloading").page();

    $j("#refresh-store").on("tap", function(){
        $j.mobile.loading( 'show');
        $j.getJSON(Static.serverPath + "getStoreCollection.php", function(data){
            Global.store.load(data);
            $j.mobile.loading( 'hide');
        });
    });
});

$j("#favorite").on("pageinit", function(event){
    Global.favorite = new Favorite();
});

$j("#bookmark").on("pageinit", function(event){
    Global.bookmark = new Bookmark();
    Global.bookmark.load();
});

$j("#downloading").on("pageinit", function(event){
    Global.downloading = new Downloading();
});


$j(function(){

});

function onDeviceReady(){
    document.addEventListener("pause", onPause, false);
    function onPause(){
        if(Global.bookmark != null){
            Global.bookmark.save();
        }
    };
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSystem){
        fileSystem.root.getFile("dummy.html", {create:true, exclusive:false}, function(fileEntry){
            var sPath = fileEntry.fullPath.replace("dummy.html", "");
            fileEntry.remove();

            Global.localPath = "file://" + sPath;
            console.log(Global.localPath);
        }, fail)
    }, fail);

    function fail(){

    }

    console.log("come to here");

    //Load collections from local
    MyTools.openTextPersistent("collections.json", function(result){
        Global.collections.load($j.evalJSON(result));
    });

//    Load Favorite from local
    MyTools.openTextPersistent("favorite.json", function(result){
        Global.favorite.load($j.evalJSON(result));
    });

    MyTools.openTextPersistent("bookmark.json", function(result){
        Global.bookmark.load($j.evalJSON(result));
    });
//    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSystem){
//        console.log("request file system comple");
//        fileSystem.root.getDirectory("TuanPhan", {create: true, exclusive: false}, function(fileEntry){
//            console.log("create dummny comp");
//            sPath = fileEntry.fullPath;
//            var fileTransfer = new FileTransfer();
//            fileEntry.remove();
//
//            fileTransfer.download(
//                "http://192.168.1.13/BookEditor/Earth.png",
//                sPath + "/Earth.png",
//                function(theFile) {
//                    console.log("download complete: " + theFile.toURI());
//
//                }, fail);
//        }, fail);
//    }, fail);
//
//    function fail(){
//        console.log("FAIL request");
//    }
//
};